package com.zonda.sampleretrofit2.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zonda.sampleretrofit2.Contants;
import com.zonda.sampleretrofit2.R;
import com.zonda.sampleretrofit2.model.NewsMediaModel;
import com.zonda.sampleretrofit2.model.NewsModel;

import java.util.ArrayList;


public class NewsListAdapter extends RecyclerView.Adapter<NewsListAdapter.NewsListViewHolder> {

  private static final String LOG_TAG = "NewsListAdapter";

  private ArrayList<NewsModel> mDatas;

  public NewsListAdapter() {
  }

  public void setDatas(ArrayList<NewsModel> datas) {
    this.mDatas = datas;
  }

  @Override
  public NewsListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    Log.i(LOG_TAG, "onCreateViewHolder viewType: " + viewType);

    final Context context = parent.getContext();

    //news_list_item1
    final View itemView = LayoutInflater.from(context).inflate(R.layout.news_list_custom_layout1,
        parent, false);
    NewsListViewHolder newsListViewHolder = new NewsListViewHolder(itemView);

//    newsListViewHolder.listImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//    newsListViewHolder.titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
//        context.getResources().getDimensionPixelSize(R.dimen.title_text_size));

//    final TestListItemLayout itemLayout = new TestListItemLayout(context);
//    NewsListViewHolder newsListViewHolder = new NewsListViewHolder(itemLayout);

    return newsListViewHolder;
  }

  @Override
  public void onBindViewHolder(final NewsListViewHolder holder, int position) {

    Log.i(LOG_TAG, "onBindViewHolder position: " + position);
    NewsModel newsModel = mDatas.get(position);

//    holder.testListItemLayout.setTitleText(newsModel.getTitle());

//    holder.titleTextView.setText(newsModel.getTitle());
//    holder.newsListTagView.setNewsAuthorText(newsModel.getAuthorName());
//    holder.newsListTagView.setNewsIssueText(newsModel.getDate());
//    holder.newsListTagView.setTagUri(newsModel.getIconUrl());
//    holder.newsListTagView.requestLayout();

//    holder.listImageView.setImageResource(R.drawable.ic_launcher);

//    final ArrayList<NewsMediaModel> mediaModels = newsModel.getThumbnailMedias();
//    if (mediaModels != null && !mediaModels.isEmpty()) {
//      holder.listImageView.setVisibility(View.VISIBLE);
//      Glide.with(Contants.APP_CONTEXT).load(mediaModels.get(0).getmUrl()).into(holder.listImageView);
//    } else {
//      holder.listImageView.setImageDrawable(null);
//      holder.listImageView.setVisibility(View.GONE);
//    }

    holder.newsListItem1Layout.setNewsAuthorName(newsModel.getAuthorName());
    holder.newsListItem1Layout.setNewsIssueTime(newsModel.getDate());
    holder.newsListItem1Layout.setNewsIconUrl(Contants.ICON_URL);//newsModel.getIconUrl()
    holder.newsListItem1Layout.setTitleText(newsModel.getTitle());
    String mediaUrl = null;
    final ArrayList<NewsMediaModel> mediaModels = newsModel.getThumbnailMedias();
    if (mediaModels != null && !mediaModels.isEmpty()) {
      mediaUrl = mediaModels.get(0).getmUrl();
    }
    holder.newsListItem1Layout.setRightImageUrl(mediaUrl);//mediaUrl Contants.TEST_PIC_URL
  }

  @Override
  public int getItemCount() {
    return mDatas == null ? 0 : mDatas.size();
  }

  @Override
  public int getItemViewType(int position) {

    return super.getItemViewType(position);
  }

  static class NewsListViewHolder extends RecyclerView.ViewHolder {

//    public final TextView titleTextView;
//    public final NewsListTagView newsListTagView;
//    public final ImageView listImageView;

//    public final TestListItemLayout testListItemLayout;

    public final NewsListItem1Layout newsListItem1Layout;

    public NewsListViewHolder(View itemView) {
      super(itemView);

      newsListItem1Layout = (NewsListItem1Layout) itemView;

//      testListItemLayout = (TestListItemLayout) itemView;

//      listImageView = (ImageView) itemView.findViewById(R.id.custom_item1_image);
//      titleTextView = (TextView) itemView.findViewById(R.id.custom_item1_title);
//      newsListTagView = (NewsListTagView) itemView.findViewById(R.id.custom_item1_tag);

//      titleTextView = (TextView) itemView.findViewById(R.id.item1_title);
//      newsListTagView = (NewsListTagView) itemView.findViewById(R.id.item1_tag_view);
//      listImageView = (ImageView) itemView.findViewById(R.id.item1_focus_image);
    }
  }
}
